﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonHelper
{
    /// <summary>
    /// 编码转换
    /// </summary>
    public static class EncodHelper
    {
        /// <summary>
        /// 获得字符串编码类型
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetEncodType(string str)
        {

            if (string.IsNullOrEmpty(str))
            {
                return "空字符串";
            }
            byte[] bytes = Encoding.Default.GetBytes(str);

            if (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF)
            {
                return "编码格式为 UTF-8";
            }
            else if (bytes[0] == 0xFE && bytes[1] == 0xFF)
            {
                return "编码格式为 Unicode";
            }
            else if (bytes[0] == 0xFF && bytes[1] == 0xFE)
            {
                return "编码格式为 Unicode（反转）";
            }
            else if (bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 0xFE && bytes[3] == 0xFF)
            {
                return "编码格式为 UTF-32（大端序）";
            }
            else if (bytes[0] == 0xFF && bytes[1] == 0xFE && bytes[2] == 0 && bytes[3] == 0)
            {
                return "编码格式为 UTF-32（小端序）";
            }
            else
            {
                return "编码格式为 ANSI";
            }
        }

        public static bool IsEncoding(DataTable dataTable, Encoding encoding)
        {
            foreach (DataRow row in dataTable.Rows)
            {
                foreach (DataColumn column in dataTable.Columns)
                {
                    if (!IsEncoding(row[column], encoding))
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        public static bool IsEncoding(object value, Encoding encoding)
        {
            if (value == DBNull.Value)
            {
                return true;
            }

            string strValue = Convert.ToString(value);
            byte[] bytes = encoding.GetBytes(strValue);
            string decodedStr = encoding.GetString(bytes);

            return string.Equals(strValue, decodedStr);
        }

        public static string GB2312ToUTF8(string str)
        {
            try
            {
                Encoding uft8 = Encoding.GetEncoding(65001);
                Encoding gb2312 = Encoding.GetEncoding("gb2312");
                byte[] temp = gb2312.GetBytes(str);
                //MessageBox.Show("gb2312的编码的字节个数：" + temp.Length);
                for (int i = 0; i < temp.Length; i++)
                {
                    //MessageBox.Show(Convert.ToUInt16(temp[i]).ToString());
                    Convert.ToUInt16(temp[i]).ToString();
                }
                byte[] temp1 = Encoding.Convert(gb2312, uft8, temp);
                //MessageBox.Show("uft8的编码的字节个数：" + temp1.Length);
                for (int i = 0; i < temp1.Length; i++)
                {
                    Convert.ToUInt16(temp1[i]).ToString();
                }
                string result = uft8.GetString(temp1);
                return result;
            }
            catch (Exception ex)//(UnsupportedEncodingException ex)
            {
                ex.ToString();
                return null;
            }
        }

        public static string UTF8ToGB2312(string str)
        {
            try
            {
                Encoding utf8 = Encoding.GetEncoding(65001);
                Encoding gb2312 = Encoding.GetEncoding("gb2312");//Encoding.Default ,936
                byte[] temp = utf8.GetBytes(str);
                byte[] temp1 = Encoding.Convert(utf8, gb2312, temp);
                string result = gb2312.GetString(temp1);
                return result;
            }
            catch (Exception ex)//(UnsupportedEncodingException ex)
            {
                //MessageBox.Show(ex.ToString());
                return null;
            }
        }
    }
}
